package com.yan.deezer.network;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;

import com.yan.deezer.object.Album;
import com.yan.deezer.object.Artist;

public class ParsingManager extends BasicParser implements DTD {
	private static final String TAG = "DEEZER_YAN_PARSING_MANAGER";

	public static List<Artist> buildArtistList(String json) {
	 
		Log.d(TAG, json);
		List<Artist> artistList = new ArrayList<Artist>();
		Artist artist;
		int startOffset = json.indexOf("[");
		try {
			JSONArray jsonArray = new JSONArray(json.substring(startOffset));
			int nbJSONObject = jsonArray.length();
			JSONObject jsonObject;
			for (int i = 0; i < nbJSONObject; ++i) {
				jsonObject = jsonArray.getJSONObject(i);
				artist = buildArtist(jsonObject);
				artistList.add(artist);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return artistList;
	}

	
	
	public static List<Album> buildArtistAlbumList(String json) {
		 
		Log.d(TAG, json);
		List<Album> artistAlbumList = new ArrayList<Album>();
		Album album;
		int startOffset = json.indexOf("[");
		try {
			JSONArray jsonArray = new JSONArray(json.substring(startOffset));
			int nbJSONObject = jsonArray.length();
			JSONObject jsonObject;
			for (int i = 0; i < nbJSONObject; ++i) {
				jsonObject = jsonArray.getJSONObject(i);
				album = buildAlbum(jsonObject);
				artistAlbumList.add(album);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return artistAlbumList;
	}
	/*
	 * public static Article buildFilmDetailed(String json){ Article film;
	 * JSONObject jsonObject; JSONArray jsonArray;
	 * 
	 * //keep only the data of the films int startOffset = json.indexOf("{",
	 * json.indexOf(FILM_DETAILS)); try{ Debug.debugTemporaryLargeString(TAG,
	 * json.substring(startOffset).substring(1515)); jsonObject = new
	 * JSONObject(json.substring(startOffset)); film = buildFilm(jsonObject);
	 * film.setDescription(getString(jsonObject, FILM_DESCRIPTION));
	 * 
	 * int indexPhotos = json.indexOf(FILM_PHOTOS); List<String> photos =
	 * buildPhotos(getBlock(json, json.indexOf('[', indexPhotos), '[', ']'));
	 * film.setPhotos(photos);
	 * 
	 * } catch (Exception e){ e.printStackTrace(); return null; } return film; }
	 */

	/*public static List<String> buildPhotos(String json) {
		List<String> photos = new ArrayList<String>();
		String photo;
		Debug.debugTemporary(TAG, "buildPhotos json = " + json);
		try {
			int startPhotoOffset;
			int endPhotoOffset;
			// for each block photo url :
			while ((startPhotoOffset = json.indexOf('"')) > -1) {
				Debug.debugTemporary(TAG, "buildPhotos startPhotoOffset = "
						+ startPhotoOffset);
				endPhotoOffset = json.indexOf('"', startPhotoOffset + 1);
				Debug.debugTemporary(TAG, "buildPhotos endPhotoOffset = "
						+ endPhotoOffset);
				photo = json.substring(startPhotoOffset + 1, endPhotoOffset);
				photos.add(photo);
				json = json.substring(endPhotoOffset + 1);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return photos;
	}*/

	private static Artist buildArtist(JSONObject jsonObject) {


		Artist mArtist = new Artist();

		try {
			mArtist.setId(getString(jsonObject, ARTIST_ID));
			mArtist.setName(getString(jsonObject, ARTIST_NAME));
			mArtist.setLink(getString(jsonObject, ARTIST_LINK));
			mArtist.setPicture(getString(jsonObject, ARTIST_PICTURE));
			mArtist.setNb_album(getString(jsonObject, ARTIST_NB_ALBUM));
			mArtist.setNb_fan(getString(jsonObject, ARTIST_NB_FAN));
			mArtist.setRadio(getString(jsonObject, ARTIST_RADIO));
			mArtist.setType(getString(jsonObject, ARTIST_TYPE));
		} 
		
		catch (Exception e)

		{
			e.printStackTrace();

			return null;

		}

		return mArtist;

	}
	

	private static Album buildAlbum(JSONObject jsonObject) {


		Album album = new Album();

		try {
			album.setId(getString(jsonObject, ALBUM_ID));
			album.setArtistString(getString(jsonObject, ALBUM_ARTIST));
			album.setLink(getString(jsonObject, ALBUM_LINK));
			album.setCover(getString(jsonObject, ALBUM_COVER));
			album.setDuration(getString(jsonObject, ALBUM_DURATION));
			album.setFans(getString(jsonObject, ALBUM_FANS));
			album.setLabel(getString(jsonObject, ALBUM_LABEL));
			album.setTitle(getString(jsonObject, DTD.ALBUM_TITLE));
			album.setRelease_date(getString(jsonObject, DTD.ALBUM_RELEASE_DATE));

		} 
		
		catch (Exception e)

		{
			e.printStackTrace();

			return null;

		}

		return album;

	}
	
	

	/*public static List<Object> buildSearchResult(String json) {
		
		
		List<Object> result = new ArrayList<Object>();
		JSONObject jsonObject;
		JSONArray jsonArray;
		int nbJSONObject;
		int startOffset = json.indexOf("[");
		try {
			List<Artist> films = new ArrayList<Artist>();
			jsonArray = new JSONArray(json.substring(startOffset));
			nbJSONObject = jsonArray.length();
			for (int i = 0; i < nbJSONObject; ++i) {
				jsonObject = jsonArray.getJSONObject(i);
				films.add(buildArtist(jsonObject));
			}

			startOffset = json.indexOf("[", json.indexOf(SEARCH_SCORES));
			jsonArray = new JSONArray(json.substring(startOffset));
			nbJSONObject = jsonArray.length();
			ArrayList<Double> scores = new ArrayList<Double>();
			ArrayList<String> scoresLabel = new ArrayList<String>();
			for (int i = 0; i < nbJSONObject; ++i) {
				int size = scores.size();
				jsonObject = jsonArray.getJSONObject(i);
				double value = jsonObject.getDouble(SEARCH_SCORES_SCORE);
				if (size == 0) {
					scores.add(value);
					scoresLabel.add(jsonObject.getString(SEARCH_SCORES_NAME));
				} else {
					for (int j = 0; j < size; ++j) {
						if (value > scores.get(j)) {
							scores.set(j, value);
							scoresLabel.set(j,
									jsonObject.getString(SEARCH_SCORES_NAME));
						}
					}
					if (size == scores.size()) {
						scores.add(value);
						scoresLabel.add(jsonObject
								.getString(SEARCH_SCORES_NAME));
					}
				}
			}

			for (int i = 0; i < nbJSONObject; ++i) {
				String label = scoresLabel.get(i);
				if (label.equals(SEARCH_TITLES)) {
					Debug.debugTemporary(TAG, "add SEARCH_TITLES");
					result.addAll(films);
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return result;
	}*/

}